
var qcloud = require('../vendor/wafer2-client-sdk/index')
var config = require('../config')
var util = require('../utils/util.js')

const recordActions = {
  get: function(openId, callback) {
    qcloud.request({
      url: `${config.service.recordUrl}?open_id=${openId}`,
      login: true,
      success(result) {
        callback(result)
      },
      fail(error) {
        util.showModel('请求失败', error)
        console.log('request fail', error)
      }
    })
  },
  update: function(record, callback) {
    qcloud.request({
      url: config.service.recordUrl,
      login: true,
      method: 'Put',
      data: record,
      success(result) {
        callback(result)
      },
      fail(error) {
        util.showModel('请求失败', error)
        console.log('request fail', error)
      }
    })
  }
}

const recordService = {
  get: function(callback) {
    const userInfo = getApp().globalData.userInfo
    recordActions.get(userInfo.openId, function (result) {
      if (result.data && result.data.data) {
        getApp().globalData.record = result.data.data
        if (callback) {
          callback(result.data.data)
        }
      }
    })
  },
  update: recordActions.update
}

module.exports = recordService
